* ==============================================================================
* 		Date: September 2023
* 		Paper: Exposure to worrisome topics can increase cognitive performance
*		Version: Preliminary version at submission state
*
* This program runs the main analysis and outputs the figures in the main text.  
* 
* database used: worries_cognitive_performance.dta
* output: Figures 1 - 3
*
* ==============================================================================


clear

** !!!!!!!!!!!!!! add your working directory here ******************************
***** Working directory strucure:
****** Folders "Data"; "Codes"; "Output"
****** Data folder contains the raw dataset "base_anonym_AMU.xlsx"
****** Output folder contains following subfolders: "main figures"; "main tables"; "SI figures"; "SI tables"

use "Data/worries_cognitive_performance", clear

* Drop pre-registered exclusions
drop if exclusions


*-------------- Globals --------------*
global figure "Output/main figures"


global controls woman scholarship undergrad i.field  age Frm
global controls2 i.weekday i.week i.time  tired i.frspeaker  frborn parents_frborn 

	
		********************************************************************			  
		*Figure 1. Emotional states before and after the topic treatments  *
		********************************************************************			
*----- Good - bad
reg ms_gb_std ib3.treatment_after $controls if finish==1, robust 
margins, at (treatment_after == (0 1 2 3))
marginsplot,recast(scatter) graphregion(fcolor(white)) xscale(r(-0.5 3.5)) nolabel xtitle("") ytitle("Linear Prediction")  ciopts(color(navy)) plotopts(mcolor(navy))  ///
title("Feeling Good", box bexpand nospan fcolor(white) size(medsmall) margin(vsmall)) scheme(s1color)  ylabel(, grid) ///
xlabel(0 `""Before" "article""' 1 `""Labor" "market""'  2 `""Mental" "health""'  3 `""Both" "controls""', labsize(medium)) ///
saving("${figure}/emotions_good",replace)

*----- awake - tired
reg ms_at_std ib3.treatment_after $controls if finish==1, robust 
margins, at (treatment_after == (0 1 2 3))
marginsplot,recast(scatter) graphregion(fcolor(white)) xscale(r(-0.5 3.5)) nolabel xtitle("") ytitle(" ")  ciopts(color(navy)) plotopts(mcolor(navy))  ///
title("Feeling Awake", box bexpand nospan fcolor(white) size(medsmall) margin(vsmall)) scheme(s1color)  ylabel(, grid) ///
xlabel(0 `""Before" "article""' 1 `""Labor" "market""'  2 `""Mental" "health""'  3 `""Both" "controls""', labsize(medium)) ///
saving("${figure}/emotions_awake",replace)

*----- calm - nervous
reg ms_cn_std ib3.treatment_after $controls if finish==1, robust 
margins, at (treatment_after == (0 1 2 3))
marginsplot,recast(scatter) graphregion(fcolor(white)) xscale(r(-0.5 3.5)) nolabel xtitle("") ytitle(" ")  ciopts(color(navy)) plotopts(mcolor(navy))  ///
title("Feeling Calm", box bexpand nospan fcolor(white) size(medsmall) margin(vsmall)) scheme(s1color)  ylabel(, grid) ///
xlabel(0 `""Before" "article""' 1 `""Labor" "market""'  2 `""Mental" "health""'  3 `""Both" "controls""', labsize(medium)) ///
saving("${figure}/emotions_calm",replace)


graph combine "${figure}/emotions_good" ///
              "${figure}/emotions_awake" ///
              "${figure}/emotions_calm", ///
			   cols(3) graphregion(fcolor(white) lcolor(white)) xsize(6.5) ysize(2.7) iscale(.95)
graph export "${figure}/fig_emotions_std.png", width(1500) height(600) replace 

eststo clear
estimates clear	



		********************************************************************			  
		*Figure 2. Treatment effect on cognitive performance				*
		********************************************************************
		
* base line controls - Piece rate treatment
eststo: reg Srm ib3.treat1 $controls if finish ==1 & payment==1, robust 
sum Srm if treat1==3 & finish ==1 & payment==1
estadd scalar mean_control `r(mean)'
estadd local Baseline "yes"
estadd local Extended "no"
estimates store reg1a
		
	
* base line controls - Threshold treatment
eststo: reg Srm ib3.treat1 $controls if finish ==1 & payment==0, robust 
sum Srm if treat1==3 & finish ==1 & payment==0
estadd scalar mean_control `r(mean)'
estadd local Baseline "yes"
estadd local Extended "no"
estimates store reg1c	

coefplot reg1a,  bylabel("Piece-rate payment")  || ///
		 reg1c,  bylabel("Threshold payment")   || ///	
		 , xline(0, lpattern(dash) lcolor(gs12)) levels(95 90) ciopts(recast(. rcap) color(navy navy)) mcolor(navy navy) msize(medium) aspectratio(1.1) ylab(, labs(medlarge)) ///
		  keep(*.treat1) label  scheme(s1color) subtitle(, size(medlarge) fcolor(white)) xtitle("Average Treatment Effect", size(medsmall)) xsize(4) ysize(2.7)		  		  
graph export "${figure}/fig_cognitive_performance.png", width(1400) height(800) replace	




		*************************************************************************************************			  
		*Figure 3. Treatment effect on cognitive performance: Pre-registered heterogeneity				*
		*************************************************************************************************
			**************************************
			****** Panel a Piece-rate ************
			**************************************
			
*----- Gender
* Reg Woman
eststo r1: reg Srm labor_market mental_health $controls if woman==1 & finish==1 & payment==1, robust
sum Srm if woman==1 & finish==1 & payment==1
* Reg Man
eststo r2: reg Srm labor_market mental_health $controls if woman==0 & finish==1 & payment==1, robust
sum Srm if woman==0 & finish==1 & payment==1


* Reg With scholarship
eststo r3: reg Srm labor_market mental_health $controls if bourse==1 & finish==1 & payment==1, robust
sum Srm if bourse==1 & finish==1 & payment==1
* Reg Withouth scholarship
eststo r4: reg Srm labor_market mental_health $controls if bourse==0 & finish==1 & payment==1, robust
sum Srm if bourse==0 & finish==1 & payment==1


* Reg 1st year student
eststo r5: reg Srm labor_market mental_health $controls if undergrad1==1 & finish==1 & payment==1, robust
sum Srm if undergrad1==1 & finish==1 & payment==1
* Reg Not 1st year student
eststo r6: reg Srm labor_market mental_health $controls if undergrad1==0 & finish==1 & payment==1, robust
sum Srm if undergrad1==0 & finish==1 & payment==1

* Reg Close to LM
eststo r7: reg Srm labor_market mental_health $controls if labormarket==1 & finish==1 & payment==1, robust
sum Srm if labormarket==1 & finish==1 & payment==1
* Reg Not Close to LM
eststo r8: reg Srm labor_market mental_health $controls if labormarket==0 & finish==1 & payment==1, robust
sum Srm if labormarket==0 & finish==1 & payment==1

* Reg Health students
eststo r9: reg Srm labor_market mental_health $controls if field==1 & finish==1 & payment==1, robust
sum Srm if field==1 & finish==1 & payment==1
* Reg Not Health students
eststo r10: reg Srm labor_market mental_health $controls if field!=1 & finish==1 & payment==1, robust
sum Srm if field!=1 & finish==1 & payment==1

* Reg  With depression
eststo r11: reg Srm labor_market mental_health $controls if depression_score==1 & finish==1 & payment==1, robust
sum Srm if depression_score==1 & finish==1 & payment==1
* Reg Without depression
eststo r12: reg Srm labor_market mental_health $controls if depression_score==0 & finish==1 & payment==1, robust
sum Srm if depression_score==0 & finish==1 & payment==1

* Reg With anxiety
eststo r13: reg Srm labor_market mental_health $controls if anxiety_score2==1 & finish==1 & payment==1, robust
sum Srm if anxiety_score2==1 & finish==1 & payment==1
* Reg Without anxiety
eststo r14: reg Srm labor_market mental_health $controls if anxiety_score2==0 & finish==1 & payment==1, robust
sum Srm if anxiety_score2==0 & finish==1 & payment==1

* Reg After qst
eststo r15: reg Srm labor_market mental_health $controls if random_emotions==1 & finish==1 & payment==1, robust
sum Srm if random_emotions==1 & finish==1 & payment==1
* Reg Before qst
eststo r16: reg Srm labor_market mental_health $controls if random_emotions==0 & finish==1 & payment==1, robust
sum Srm if random_emotions==0 & finish==1 & payment==1


*-------------- heterogeneity - Piece rate --------------*
* !!! We need to delete legend manually, some scaling was adjusted manually

coefplot (r1, msymbol(o) ciopts(recast(. rcap) color(maroon maroon)) mcolor(maroon) mlabels(labor_market= 0.5 "Woman" mental_health= 0.5 "Woman" ) mlabcolor(maroon))  ///
		 (r2, msymbol(o) ciopts(recast(. rcap) color(navy navy)) mcolor(navy) mlabcolor(navy) mlabels(labor_market= 0.5 "Man" mental_health= 0.5 "Man")) , legend(off)  bylabel("Gender")   || ///
		 (r3, mlabels(labor_market= 1 "With" mental_health= 1 "With" ))  (r4, mlabels(labor_market= 1 "Without" mental_health= 1 "Without")), legend(off) bylabel("Scholarship") || /// 
		 (r5, mlabels(labor_market= 1 "1st year" mental_health= 1 "1st year" ))  (r6, mlabels(labor_market= 1 "Not 1st year" mental_health= 1 "Not 1st year")),legend(off) bylabel("Level of Study") || /// 
		 (r7, mlabels(labor_market= 1 "Close" mental_health= 1 "Close" ))  (r8, mlabels(labor_market= 1 "Not close" mental_health= 1 "Not close")), legend(off) bylabel("Close to Labor Market") || /// 
		 (r9, mlabels(labor_market= 1 "Health" mental_health= 1 "Health" ))  (r10, mlabels(labor_market= 1 "Other" mental_health= 1 "Other")), legend(off) bylabel("Field of Study") || /// 
		 (r11, mlabels(labor_market= 1 "Above median" mental_health= 1 "Above median" ))  (r12, mlabels(labor_market= 1 "Below median" mental_health= 1 "Below median")), legend(off) bylabel("Depression score") || /// 
		 (r13, mlabels(labor_market= 1 "Above median" mental_health= 1 "Above median" ))  (r14, mlabels(labor_market= 1 "Below median" mental_health= 1 "Below median")), legend(off) bylabel("Anxiety score") || /// 
		 (r15, mlabels(labor_market= 1 "After" mental_health= 1 "After" ))  (r16, mlabels(labor_market= 1 "Before" mental_health= 1 "Before")), legend(off) bylabel("Emotion questionnaire") || ///  
		 , bycoef xline(0, lpattern(dash) lcolor(gs12)) levels(95 90) ciopts(recast(. rcap) color(navy navy)) mcolor(navy navy) legend(off)  ///
		  keep(labor_market mental_health) scheme(s1color) subtitle(, size(medsmall) fcolor(white)) xtitle("Average Treatment Effect by Treatment", size(small)) 
graph export "${figure}/fig_srm_het_piecerate.png",replace


eststo clear
estimates clear	



			**************************************
			****** Panel b Threshold *************
			**************************************
			
* Reg Woman
eststo r1: reg Srm labor_market mental_health $controls if woman==1 & finish==1 & payment==0, robust
sum Srm if woman==1 & finish==1 & payment==0

* Reg Man
eststo r2: reg Srm labor_market mental_health $controls if woman==0 & finish==1 & payment==0, robust
sum Srm if woman==0 & finish==1 & payment==0

* Reg With scholarship
eststo r3: reg Srm labor_market mental_health $controls if bourse==1 & finish==1 & payment==0, robust
sum Srm if bourse==1 & finish==1 & payment==0
* Reg Withouth scholarship
eststo r4: reg Srm labor_market mental_health $controls if bourse==0 & finish==1 & payment==0, robust
sum Srm if bourse==0 & finish==1 & payment==0


* Reg 1st year student
eststo r5: reg Srm labor_market mental_health $controls if undergrad1==1 & finish==1 & payment==0, robust
sum Srm if undergrad1==1 & finish==1 & payment==0

* Reg Not 1st year student
eststo r6: reg Srm labor_market mental_health $controls if undergrad1==0 & finish==1 & payment==0, robust
sum Srm if undergrad1==0 & finish==1 & payment==0


* Reg Close to LM
eststo r7: reg Srm labor_market mental_health $controls if labormarket==1 & finish==1 & payment==0, robust
sum Srm if labormarket==1 & finish==1 & payment==0
* Reg Not Close to LM
eststo r8: reg Srm labor_market mental_health $controls if labormarket==0 & finish==1 & payment==0, robust
sum Srm if labormarket==0 & finish==1 & payment==0



* Reg Health students
eststo r9: reg Srm labor_market mental_health $controls if field==1 & finish==1 & payment==0, robust
sum Srm if field==1 & finish==1 & payment==0
* Reg Not Health students
eststo r10: reg Srm labor_market mental_health $controls if field!=1 & finish==1 & payment==0, robust
sum Srm if field!=1 & finish==1 & payment==0


* Reg  With depression
eststo r11: reg Srm labor_market mental_health $controls if depression_score==1 & finish==1 & payment==0, robust
sum Srm if depression_score==1 & finish==1 & payment==0


* Reg Without depression
eststo r12: reg Srm labor_market mental_health $controls if depression_score==0 & finish==1 & payment==0, robust
sum Srm if depression_score==0 & finish==1 & payment==0


* Reg With anxiety
eststo r13: reg Srm labor_market mental_health $controls if anxiety_score2==1 & finish==1 & payment==0, robust
sum Srm if anxiety_score2==1 & finish==1 & payment==0
* Reg Without anxiety
eststo r14: reg Srm labor_market mental_health $controls if anxiety_score2==0 & finish==1 & payment==0, robust
sum Srm if anxiety_score2==0 & finish==1 & payment==0


* Reg After qst
eststo r15: reg Srm labor_market mental_health $controls if random_emotions==1 & finish==1 & payment==0, robust
sum Srm if random_emotions==1 & finish==1 & payment==0
* Reg Before qst
eststo r16: reg Srm labor_market mental_health $controls if random_emotions==0 & finish==1 & payment==0, robust
sum Srm if random_emotions==0 & finish==1 & payment==0

* !!! We need to delete legend manually, some scaling was adjusted manually
coefplot (r1, msymbol(o) ciopts(recast(. rcap) color(maroon maroon)) mcolor(maroon) mlabels(labor_market= 0.5 "Woman" mental_health= 0.5 "Woman" ) mlabcolor(maroon))  ///
		 (r2, msymbol(o) ciopts(recast(. rcap) color(navy navy)) mcolor(navy) mlabcolor(navy) mlabels(labor_market= 0.5 "Man" mental_health= 0.5 "Man")) , legend(off)  bylabel("Gender")   || ///
		 (r3, mlabels(labor_market= 1 "With" mental_health= 1 "With" ))  (r4, mlabels(labor_market= 1 "Without" mental_health= 1 "Without")), legend(off) bylabel("Scholarship") || /// 
		 (r5, mlabels(labor_market= 1 "1st year" mental_health= 1 "1st year" ))  (r6, mlabels(labor_market= 1 "Not 1st year" mental_health= 1 "Not 1st year")),legend(off) bylabel("Level of Study") || /// 
		 (r7, mlabels(labor_market= 1 "Close" mental_health= 1 "Close" ))  (r8, mlabels(labor_market= 1 "Not close" mental_health= 1 "Not close")), legend(off) bylabel("Close to Labor Market") || /// 
		 (r9, mlabels(labor_market= 1 "Health" mental_health= 1 "Health" ))  (r10, mlabels(labor_market= 1 "Other" mental_health= 1 "Other")), legend(off) bylabel("Field of Study") || /// 
		 (r11, mlabels(labor_market= 1 "Above median" mental_health= 1 "Above median" ))  (r12, mlabels(labor_market= 1 "Below median" mental_health= 1 "Below median")), legend(off) bylabel("Depression score") || /// 
		 (r13, mlabels(labor_market= 1 "Above median" mental_health= 1 "Above median" ))  (r14, mlabels(labor_market= 1 "Below median" mental_health= 1 "Below median")), legend(off) bylabel("Anxiety score") || /// 
		 (r15, mlabels(labor_market= 1 "After" mental_health= 1 "After" ))  (r16, mlabels(labor_market= 1 "Before" mental_health= 1 "Before")), legend(off) bylabel("Emotion questionnaire") || ///  
		 , bycoef xline(0, lpattern(dash) lcolor(gs12)) levels(95 90) ciopts(recast(. rcap) color(navy navy)) mcolor(navy navy) legend(off)  ///
		  keep(labor_market mental_health) scheme(s1color) subtitle(, size(medsmall) fcolor(white)) xtitle("Average Treatment Effect by Treatment", size(small)) 
graph export "${figure}/fig_srm_het_threshold.png",replace


eststo clear
estimates clear	
